<?php
class Image extends AppModel{
    public $useTable  = 'image';
    public $primaryKey  = 'image_id';
    
    function add($post_id = "", $type="", $data = array())
    {
        if(empty($data))
            return null;
        
        $image = array();
        $main_image = $data['main_image'];
        
        if($this->edit_url_image($main_image) != "")
        {
            $image['post_id'] = $post_id;
            $image['name'] = $this->edit_url_image($main_image);
            $image['type'] = $type;
            $image['status'] = '1';
            $this->save($image);
        }
        
        

        $other_image = $data['other_image'];
        $order_image = $data['order_image'];
        foreach($other_image as $key => $url_img)
        {
            if($this->edit_url_image($url_img) != "")
            {
                $image['post_id'] = $post_id;
                $image['name'] = $this->edit_url_image($url_img);
                $image['type'] = $type;
                $image['status'] = '0';
                $image['sort_order'] = $order_image[$key];
                $this->create();
                $this->save($image);
            }
        }
    }
    
    function edit($post_id = "", $type="", $data = array())
    {
        $this->deleteAll(array('post_id' => $post_id, 'type' => $type));
        $this->add($post_id, $type, $data);
    }
    
    function get($post_id = "", $type = "")
    {
        $sql = array("conditions"=> array(
                                            "post_id"=> "$post_id",
                                            "type"=> "$type",
                                            "status"=> "1"
        								),
        			"order" => array('sort_order' => 'asc')
		         );
        $data = $this->find("all", $sql);
        $result = array();
        if(!empty($data))
        {
            $result['main_image'] = $data[0]['Image'];
        }
        
        $sql = array("conditions"=> array(
                                            "post_id"=> "$post_id",
                                            "type"=> "$type",
                                            "status"=> "0"
        								),
        			"order" => array('sort_order' => 'asc')
		         );
        $data = $this->find("all", $sql);

        if(!empty($data))
        {
            foreach($data as $value)
            {
                $result['other_image'][] = $value['Image'];
            }
        }
        return $result;
    }
    
    function edit_url_image($url = null)
    {
        $path_folder_upload = Configure::read('path_folder_upload');
        $arr_url_img = explode('/', $url);
        $name_img = end($arr_url_img);
        $i = 0;
        if($name_img != 'no-image.jpg')
        {
            $url_folder = "";
            foreach($arr_url_img as $key => $value)
            {
                if($i == 1 && $value != $name_img)
                {
                    $url_folder .= $value.'/';
                }
                if($value == 'upload')
                    $i = 1;
            }
            return $path_folder_upload . $url_folder . $name_img;
        }
        else
        {
            return "";
        }
    }

    
    
}